package com.system.insurance.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.common.result.Result;
import com.system.insurance.entity.ContentSetting;
import com.system.insurance.entity.SmallInsuranceType;
import com.system.insurance.entity.StarScheme;
import com.system.insurance.service.StarSchemeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 明星方案 前端控制器
 * </p>
 *
 * @author WS
 * @since 2024-05-10
 */
@Api(tags = "明星方案")
@RestController
@RequestMapping("/star-scheme")
public class StarSchemeController {


    @Autowired
    private StarSchemeService starSchemeService;


    @ApiOperation(value = "查看所有明星方案")
    @GetMapping("/starSchemeList")
    public Result<List<StarScheme>> starSchemeList(){
        List<StarScheme> list = starSchemeService.list();
        return Result.ok(list);
    }

    @ApiOperation(value = "查看单个明星方案")
    @GetMapping("/starSchemeOne/{id}")
    public Result<StarScheme> starSchemeOne(@PathVariable Integer id){
        StarScheme  one= starSchemeService.getById(id);
        return Result.ok(one);
    }


    @ApiOperation(value = "删除明星方案")
    @PostMapping("/deleteStarScheme")
    public Result deleteStarScheme(@RequestBody StarScheme starScheme){
        boolean remove = starSchemeService.removeById(starScheme.getId());

        if (remove){
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }


    @ApiOperation(value = "添加明星方案")
    @PostMapping("/saveStarScheme")
    public Result<StarScheme> saveStarScheme(@RequestBody StarScheme starScheme){
        starScheme.setCreateTime(LocalDateTime.now());
        boolean save = starSchemeService.save(starScheme);

        if (save){
            return Result.ok(starScheme).message("添加成功");
        }
        return Result.fail(starScheme).message("添加失败");
    }

    @ApiOperation(value = "修改明星方案")
    @PostMapping("/updateStarScheme")
    public Result<StarScheme> updateStarScheme(@RequestBody StarScheme starScheme){
        starScheme.setUpdateTime(LocalDateTime.now());
        boolean update = starSchemeService.updateById(starScheme);

        if (update){
            return Result.ok(starScheme).message("修改成功");
        }
        return Result.fail(starScheme).message("修改失败");
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/{page}/{limit}")
    public Result<IPage<StarScheme>> index124(@PathVariable Long page, @PathVariable Long limit, StarScheme subject){
        Page<StarScheme> recommendPage = new Page<>(page,limit);
        LambdaQueryWrapper<StarScheme> wrapper = new LambdaQueryWrapper<>();
        Integer id = subject.getId();
        if (id!=null){
            wrapper.eq(StarScheme::getId,id);
        }
        IPage<StarScheme> iPage = starSchemeService.page(recommendPage,wrapper);
        return Result.ok(iPage);
    }
}

